#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <signal.h>

#include "include/sock_lib.h"
#include "include/settings.h"
#include "include/defs.h"
#include "include/sync.h"
#include "include/thread_pool.h"

int main(int argc, char** argv) {

    settings_t settings;
    int c = '\0'; //character to get

    #ifndef LINUX
        sigset_t   newmask; 
        sigset_t   oldmask; 
        sigemptyset(&newmask);
        sigaddset(&newmask, SIGPIPE);
        if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) 
        {
            fprintf(stderr, "can't set signal mask\n");
            exit(1);
        } 
    #endif

    printf("* Beginning l33t_server Initialization *\n\n");

    init_settings(&settings, argc,argv);

    printf("** Using %d threads\n",settings.MAX_THREADS);
    printf("** Using home_dir: %s\n",settings.LOAD_DIR);   
 
    if (chdir(settings.LOAD_DIR) < 0) {
        printf("Error setting home directory\n");
        exit(1);
    }


    printf("* l33t_server started *\n\n");

    #ifndef _THREAD_POOL_
        start_puyand(settings.PORT_NUMBER);
    #else
        init_synch();
        init_thread_pool(&settings);
    #endif

    while (c != 'q') {
        c = getchar();    
    }

    return EXIT_SUCCESS;
}
